/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gap.revision;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.gap.entities.GapMasterEmployee;
import com.gap.entities.GapTransactionEmployee;
import com.gap.entities.GapTransactionEmployeeDetail;
import com.gap.entities.GapTransactionRevisionDetail;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author Yunita
 */
public class RevisionDetail extends Page {

    private Field revisionDetailId;
    private Field revisionDetailItemId;
    private Field revisionDetailPrice;
    private Field revisionDetailDiscount;
    private Field revisionDetailNett;
    private Field revisionDetailDescription;

    public RevisionDetail() {
        super();

        setTheClass(GapTransactionRevisionDetail.class);

        revisionDetailId = new Field();
        revisionDetailId.setName("revisionDetailId");
        revisionDetailId.setCaption("id");
        //posId.setInfo("pos id");
        revisionDetailId.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        revisionDetailId.setInputProperties(" size=\"20\"");
        getFields().add(revisionDetailId);

        revisionDetailItemId = new Field();
        revisionDetailItemId.setName("revisionDetailItemId");
        revisionDetailItemId.setCaption("item");
        revisionDetailItemId.setRequired(true);
        revisionDetailItemId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        revisionDetailItemId.setInputProperties(" size=\"30\" ");
        getFields().add(revisionDetailItemId);

        revisionDetailDiscount = new Field();
        revisionDetailDiscount.setName("revisionDetailDiscount");
        revisionDetailDiscount.setCaption("discount percent");
        revisionDetailDiscount.setRequired(true);
//        posDetailDiscount.setDefaultValue(0);
//        posDiscount.setFormater(MoneyFormatter.getInstance());
        revisionDetailDiscount.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        revisionDetailDiscount.setInputProperties(" size=\"20\" readonly");
        getFields().add(revisionDetailDiscount);

        revisionDetailPrice = new Field();
        revisionDetailPrice.setName("revisionDetailPrice");
        revisionDetailPrice.setCaption("price");
        revisionDetailPrice.setRequired(true);
        revisionDetailPrice.setDefaultValue(0);
        revisionDetailPrice.setFormater(MoneyFormatter.getInstance());
        revisionDetailPrice.setInputCase(Field.CASE_UPPER);
        revisionDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        revisionDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(revisionDetailPrice);

        revisionDetailNett = new Field();
        revisionDetailNett.setName("revisionDetailNett");
        revisionDetailNett.setCaption("nett price");
        revisionDetailNett.setRequired(true);
        revisionDetailNett.setDefaultValue(0);
        revisionDetailNett.setFormater(MoneyFormatter.getInstance());
        revisionDetailNett.setInputCase(Field.CASE_UPPER);
        revisionDetailNett.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        revisionDetailNett.setInputProperties(" size=\"20\"");
        getFields().add(revisionDetailNett);

        revisionDetailDescription = new Field();
        revisionDetailDescription.setName("revisionDetailDescription");
        revisionDetailDescription.setCaption(" description");
        //revisionDescription.setInfo("revision description");
        revisionDetailDescription.setListProperties(" width=\"20%\"");
        revisionDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        revisionDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        getFields().add(revisionDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(revisionDetailItemId);
        getFieldsAdd().add(revisionDetailPrice);
        getFieldsAdd().add(revisionDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(revisionDetailItemId);
        getFieldsEdit().add(revisionDetailPrice);
        getFieldsEdit().add(revisionDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(revisionDetailItemId);
        getFieldsList().add(revisionDetailDiscount);
        getFieldsList().add(revisionDetailPrice);
        getFieldsList().add(revisionDetailNett);
        getFieldsList().add(revisionDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
        
        setController(new TransactionEmployeeDetailController(this));
    }
    
    
    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<GapTransactionEmployeeDetail> request;
            request = em.createQuery("SELECT t FROM GapTransactionEmployeeDetail t WHERE t.posDetailPosId.posId=" + id.toString()).setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (GapTransactionEmployeeDetail pd : request) {
                GapTransactionRevisionDetail rd = new GapTransactionRevisionDetail();
                rd.setRevisionDetailItemId(pd.getPosDetailItemId());
                rd.setRevisionDetailPrice(pd.getPosDetailPrice());
                rd.setRevisionDetailDiscount(pd.getPosDetailDiscount());
                rd.setRevisionDetailNett(pd.getPosDetailNett());
                rd.setRevisionDetailDescription(pd.getPosDetailDescription());
                datas.add(rd);
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
        }
    }
    
    class TransactionEmployeeDetailController extends Controller {

        public TransactionEmployeeDetailController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            GapTransactionRevisionDetail detail = (GapTransactionRevisionDetail) data;
            EntityManager em = getEntityManager();
            BigDecimal total = BigDecimal.ZERO;

            GapMasterEmployee employee = null;
            //---employee---
            try {
                employee = (GapMasterEmployee) em.createQuery("SELECT t FROM GapMasterEmployee t WHERE t.employeeId='" + detail.getRevisionDetailRevisionId().getRevisionEmployeeId().getEmployeeId() + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
                System.out.println(" employee  ---->" + employee);
            } catch (Exception e) {
                System.out.println(" salahe employee  ---->" + e);
            }

            total = detail.getRevisionDetailPrice();
            BigDecimal percent = BigDecimal.ZERO;
            int item = 0;
            item = detail.getRevisionDetailItemId().getItemDiscount().intValue();
            try {
                if (item == 0) {
                    percent = total.multiply(detail.getRevisionDetailDiscount().divide(new BigDecimal(100)));
                    System.out.println(" percent  1---->" + percent);
                } else {
                    percent = total.multiply(detail.getRevisionDetailItemId().getItemDiscount().divide(new BigDecimal(100)));
                    detail.setRevisionDetailDiscount(detail.getRevisionDetailItemId().getItemDiscount());
                    System.out.println(" percent  2---->" + percent +" dan discount :"+detail.getRevisionDetailDiscount());
                }
            } catch (Exception e) {
                Logger.getInstance().log(e);
                System.out.println(" salahe percent  ---->" + e);
            }
            total = total.subtract(percent);
            System.out.println(" total---->" + total);
            detail.setRevisionDetailNett(total);
        }

        @Override
        public void beforeUpdate(Object data) {
            GapTransactionRevisionDetail detail = (GapTransactionRevisionDetail) data;
            EntityManager em = getEntityManager();
            BigDecimal total = BigDecimal.ZERO;
            total = detail.getRevisionDetailPrice();
            BigDecimal percent = BigDecimal.ZERO;
            int item = 0;
            item = detail.getRevisionDetailItemId().getItemDiscount().intValue();
            try {
                if (item == 0) {
                    percent = total.multiply(detail.getRevisionDetailDiscount().divide(new BigDecimal(100)));
                    System.out.println(" percent  1---->" + percent);
                } else {
                    percent = total.multiply(detail.getRevisionDetailItemId().getItemDiscount().divide(new BigDecimal(100)));
                    detail.setRevisionDetailDiscount(detail.getRevisionDetailItemId().getItemDiscount());
                    System.out.println(" percent  2---->" + percent +" dan discount :"+detail.getRevisionDetailDiscount());
                }
            } catch (Exception e) {
                Logger.getInstance().log(e);
                System.out.println(" salahe percent  ---->" + e);
            }
            total = total.subtract(percent);
            System.out.println(" total---->" + total);
            detail.setRevisionDetailNett(total);
        }
        
        
    }
}
